/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller.dao;

import java.sql.*;
import javax.naming.*;
import javax.sql.*;

/**
 *
 * @author Carlos Alvarado
 */
final public class Connect {


  /** Uses JNDI and Datasource (preferred style).   */
  public static Connection getConnection(){
    String DATASOURCE_CONTEXT = "jdbc/examds";

    Connection result = null;
    try {
      Context initialContext = new InitialContext();
      if ( initialContext == null){
        log("JNDI problem. Cannot get InitialContext.");
      }
      DataSource datasource = (javax.sql.DataSource)initialContext.lookup(DATASOURCE_CONTEXT);
      if (datasource != null) {
        result = datasource.getConnection();
      }
      else {
        log("Failed to lookup datasource.");
      }
    }
    catch ( NamingException ex ) {
      log("Cannot get connection: " + ex);
    }
    catch(SQLException ex){
      log("Cannot get connection: " + ex);
    }
    return result;
  }

  private static void log(Object aObject){
    System.out.println(aObject);
  }
}
